package com.ybwh.springboot2.conf;

import com.ybwh.springboot2.common.redis.FastJsonCodec;
import com.ybwh.springboot2.common.redis.RedisClient;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

//@Configuration
public class RedissonConfig {

    @Value("${redis.addr}")
    private String redisAddr;
    @Value("${redis.password}")
    private String redisPassword;
    @Value("${redis.db}")
    private int db;


    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.setCodec(new FastJsonCodec());
        //config.useClusterServers().addNodeAddress("127.0.0.1:6379");
        SingleServerConfig c = config.useSingleServer();
        c.setAddress("redis://" + redisAddr);
        c.setPassword(redisPassword);
        c.setDatabase(db);

        return Redisson.create(config);
    }


    @Bean
    public RedisClient redisClient(RedissonClient redissonClient) {
        return new RedisClient(redissonClient);
    }

}
